﻿using System;
using System.Collections.Generic;
using System.Text;
using WcfServiceBomberman.Contracts.Duplex;
using WcfServiceBomberman.Contracts.Monoplex;
using BomberManServer.Utils;
using System.Linq;
using System.ServiceModel;
using WcfServiceBomberman.Callbacks;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace WcfServiceBomberman.Interfaces
{
    public class ServiceWebSite : Context.Handler, IServiceGameWebSite, IServiceData
    {
        WebSite website;
        ConcurrentDoubleBufferedLinkedList<MatchMaking.Queueable>.Producer buffer;

        public ServiceWebSite()
        {
            context.website = this;
            website = new WebSite();
            buffer = context.players.producer;
        }

        void IServiceGameWebSite.register()
        {
            // get the callback from the website
            website.callback = OperationContext.Current.GetCallbackChannel<IServiceGameWebSiteCallback>();
        }

        bool IServiceGameWebSite.addPlayerInRankedQueue(uint uid, MatchMaking.MatchType type)
        {
            if (context.matchmaker.isQueued(uid)) return false;
            context.matchmaker.queue(uid);

            WcfServiceBomberman.Models.Player playerToAdd = context.database.players.First(m => m.play_id == uid);
            // format data
            Player player = new Player((ushort)playerToAdd.play_id, playerToAdd.play_name, 0, 0, 0, 0);
            MatchMaking.Player p = new MatchMaking.Player(player, playerToAdd.play_elo);
            // register in matchmaking
            buffer.produce(new MatchMaking.Queueable(p, type));
            return true;
        }

        bool IServiceGameWebSite.createCustomGame(MatchMaking.MatchType type, CustomTeam[] teams)
        {
            // aknowledge the server
            throw new NotImplementedException();
        }

        public int createPlayer(String player_client_id, String application_id, String player_name)
        {
            // check if a player with the same name already exists
            if (context.database.players.Any(m => m.play_name == player_name))
                return 0;

            WcfServiceBomberman.Models.Player player = new WcfServiceBomberman.Models.Player();
            player.play_client_id = player_client_id;
            player.play_site_id = application_id;
            player.play_name = player_name;

            context.database.players.Add(player);
            player.play_id = (uint) context.database.players.Count;
            player.play_elo = 1000;
            context.database.save();
            return (int) player.play_id;
        }

        void IServiceData.getElo()
        {
            throw new NotImplementedException();
        }

        // public functions
        public void onGameCreated(Uri adresse, int port, GameCreatedArgs_Player[] args)
        {
            website.callback.notifyNewGame(adresse,port,args);
        }

        internal void onGameFinished(GameFinishedArgs args)
        {
            website.callback.onGameFinished(args);
        }
    }
}
